<style type="text/css">
    #businesses-college-update {
        padding: 20px 25px 25px 0;
    }
</style>

<!-- 注意id属性每个页面id必须独立-->
<div class="layui-fluid" id="businesses-college-update">
    <!-- 注意lay-filter属性，该属性为form表单唯一属性-->
    <form class="layui-form" action="" lay-filter="businesses-college-update-form">
        <!-- 隐藏当前对象id-->
        <div class="layui-form-item febs-hide">
            <label class="layui-form-label febs-form-item-require">院校id：</label>
            <div class="layui-input-block">
                <input type="text" name="id" data-th-value="${college.id}">
            </div>
        </div>
        <!-- layui-form-item为自适应长度样式 -->
        <div class="layui-form-item">
            <label class="layui-form-label febs-form-item-require">院校</label>
            <!-- layui-input-block为整行宽度样式 -->
            <div class="layui-input-block">
                <!-- minlength为最小长度、 maxlength为最大长度-->
                <!-- autocomplete默认填写off,此项为配置使用缓存读取历史记录-->
                <!-- lay-verify为表单验证属性：required：必填项、range于minlength，maxlength公用， character不能输入特殊字符-->
                <!-- 详细表单验证请参考：resources\static\febs\lay\modules\validate.js-->
                <input type="text" name="name" minlength="2" maxlength="20" data-th-id="${college.id}"
                       lay-verify="range|name" autocomplete="off" class="layui-input" readonly>
            </div>
        </div>
        <div class="layui-form-item">
            <label class="layui-form-label febs-form-item-require">简称</label>
            <div class="layui-input-block">
                <input type="text" name="simpleName" minlength="2" maxlength="20"
                       lay-verify="required|range|name|character" autocomplete="off" class="layui-input">
            </div>
        </div>
        <div class="layui-form-item">
            <label class="layui-form-label febs-form-item-require">地址</label>
            <div class="layui-input-block">
                <input type="text" name="address" minlength="4" maxlength="100"
                       lay-verify="required|range|name|character" autocomplete="off" class="layui-input">
            </div>
        </div>

        <div class="layui-form-item">
            <label class="layui-form-label febs-form-item-require">负责人</label>
            <div class="layui-input-block">
                <input type="text" name="principalName" minlength="2" maxlength="10"
                       lay-verify="required|range|name|character" autocomplete="off" class="layui-input">
            </div>
        </div>

        <div class="layui-form-item">
            <label class="layui-form-label ">联系方式</label>
            <div class="layui-input-block">
                <input type="tel" name="tel" lay-verify="phoneNum" autocomplete="off" class="layui-input">
            </div>
        </div>
        <div class="layui-form-item layui-form-text">
            <label class="layui-form-label">简介</label>
            <div class="layui-input-block">
                <textarea name="remark" maxlength="100" class="layui-textarea">
                </textarea>
            </div>
        </div>
        <div class="layui-form-item febs-hide">
            <button class="layui-btn" lay-submit="" lay-filter="college-update-form-submit"
                    id="businesses-college-update-submit-btn"></button>
        </div>
    </form>
</div>

<script data-th-inline="javascript">
    layui.use(['febs', 'form', 'validate'], function () {
        let $ = layui.$,
            febs = layui.febs,
            layer = layui.layer,
            form = layui.form,
            // 获得后台出入的实体对象
            college = [[${college}]],
            // 验证
            validate = layui.validate;
        // 表单验证
        form.verify(validate);
        // 表单渲染
        form.render();
        // 初始化页面数据
        initCollegeValue();
        // 初始化页面数据
        function initCollegeValue() {
            form.val("businesses-college-update-form", {
                "name": college.name,
                "simpleName": college.simpleName,
                "address": college.address,
                "principalName": college.principalName,
                "tel": college.tel,
                "remark": college.remark
            });
        }

        // 表单提交事件
        form.on('submit(college-update-form-submit)', function (data) {
             console.log("进入修改" + data.field)
            // 如果都没有修改直接返回
            if (febs.nativeEqual(data.field, college)) {
                febs.alert.warn('数据未作任何修改！');
                return false;
            // 如果只修改了名字之外的东西，则直接更新
            }else if(febs.nativeEqual(data.field.name, college.name)
                && febs.nativeEqual(data.field.simpleName, college.simpleName)){
                febs.post(ctx + 'college/update', data.field, function () {
                    layer.closeAll();
                    febs.alert.success('院校数据修改成功');
                    // 点击父页面查询按钮
                    $('#businesses-college').find('#businesses-college-query-btn').click();
                });
            // 如果名字发生变化先进行判断是否重名，如果重名则返回，不重名则修改
            }else {
                let name = data.field.name;
                let simpleName = data.field.simpleName;
                if(febs.nativeEqual(data.field.name, college.name)){
                    name = null;
                }
                if(febs.nativeEqual(data.field.simpleName, college.simpleName)){
                    simpleName = null;
                }
                $.ajax({
                    type: 'get',
                    url: 'college/isRepetitive',
                    data: {"name": name, "simpleName": simpleName},
                    success:function(res){
                        if(res){
                            febs.post(ctx + 'college/update', data.field, function () {
                                layer.closeAll();
                                febs.alert.success('院校数据修改成功');
                                $('#businesses-college').find('#businesses-college-query-btn').click();
                            });
                        }else{
                            febs.alert.error("输入的名称或简称重复，请检查");
                        }
                    }
                });
            }
            return false;
        });
    });
</script>